package cn.jingzhuan.element.demo.view.fragment

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutCompat
import android.support.v7.widget.LinearLayoutManager
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.FragmentFmAudioListBinding
import cn.jingzhuan.fm.databinding.ItemFmAudioBinding
import cn.jingzhuan.fm.mvp.contract.FMAudioListContract
import cn.jingzhuan.fm.mvp.presenter.FMAudioListPresenter
import cn.jingzhuan.fm.pojo.FMAlbum
import cn.jingzhuan.fm.pojo.FMAudio
import cn.jingzhuan.fm.ui.activity.FMAlbumDetailActivity
import cn.jingzhuan.fm.ui.activity.FMAudioActivity
import cn.jingzhuan.fm.ui.adapter.BaseBindingRecyclerAdapter
import cn.jingzhuan.fm.ui.adapter.BindingRecyclerViewHolder
import cn.jingzhuan.fm.ui.fragment.BaseBindingFragment
import com.jcodecraeer.xrecyclerview.ProgressStyle
import com.jcodecraeer.xrecyclerview.XRecyclerView
import javax.inject.Inject


class FMAudioListFragment : BaseBindingFragment<FragmentFmAudioListBinding>(),FMAudioListContract.View {

    @Inject
    lateinit var audioListPresenter: FMAudioListPresenter
    lateinit var album : FMAlbum
    var audioList = mutableListOf<FMAudio>()

    override fun getLayoutId(): Int = R.layout.fragment_fm_audio_list

    override fun onDataBind(savedInstanceState: Bundle?, binding: FragmentFmAudioListBinding) {
        val activity = activity as FMAlbumDetailActivity
        album = activity.album
        initView()
        initData()
        initListener()
    }

    private fun initListener() {
        // 排序
        mBinding.audioChangeOrder.setOnClickListener {
            audioListPresenter.reverse()
        }
    }


    private fun initData() {
        audioList.clear()
        audioListPresenter.getAudioList(album.id)
    }

    private fun initView() {
        val layoutManager = LinearLayoutManager(context, LinearLayoutCompat.VERTICAL, false)
        this.mBinding.recyclerView.layoutManager = layoutManager
        this.mBinding.recyclerView.adapter = object: BaseBindingRecyclerAdapter<FMAudio>(context!!, audioList){

            override fun getItemLayoutId(viewType: Int): Int  = R.layout.item_fm_audio

            override fun bindData(holder: BindingRecyclerViewHolder, position: Int, item: FMAudio) {
                var fmAudioBinding = holder.binding as ItemFmAudioBinding
                fmAudioBinding.audio = item
                holder.itemView.setOnClickListener {
                    FMAudioActivity.startActivity(holder.itemView.context, item.id)
                }
            }
        }
        val dividerItemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(context!!, R.drawable.shape_divider)!!)
        this.mBinding.recyclerView.addItemDecoration(dividerItemDecoration)

        mBinding?.recyclerView?.setLoadingListener(object : XRecyclerView.LoadingListener {
            override fun onLoadMore() {
                audioListPresenter.loadMore()
            }

            override fun onRefresh() {
                audioListPresenter.refresh()
            }
        })
        mBinding?.recyclerView?.setLoadingMoreEnabled(true)
        mBinding?.recyclerView?.defaultRefreshHeaderView?.setRefreshTimeVisible(true)
        mBinding?.recyclerView?.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin)
        mBinding?.recyclerView?.setLimitNumberToCallLoadMore(2)
    }

    override fun onDataInit() {
        audioList.clear()
        mBinding?.recyclerView?.setLoadingMoreEnabled(true)
    }

    override fun onRefresh() {
        onDataInit()
        mBinding.recyclerView.refreshComplete()
    }

    override fun onDataChange(data : List<FMAudio>) {
        audioList.addAll(data)
        this.mBinding.recyclerView.adapter.notifyDataSetChanged()
    }

    override fun onLoadMoreComplete() {
        mBinding.recyclerView.loadMoreComplete()
    }

    override fun onNoMoreData() {
        mBinding?.recyclerView?.setLoadingMoreEnabled(false)
    }

    override fun onReverse(isReverse: Boolean) {
        if (isReverse) {
            mBinding.audioChangeOrder.isSelected = false
            mBinding.audioChangeOrder.text = resources.getText(R.string.album_order_desc)
        } else {
            mBinding.audioChangeOrder.isSelected = true
            mBinding.audioChangeOrder.text = resources.getText(R.string.album_order_asc)
        }
    }

}